from collections import OrderedDict

from apps.goods.models import GoodsChannel


def get_categories():
    # 先用group_id排序  如果一样用sequence(组内顺序)
    channels = GoodsChannel.objects.order_by("group_id", "sequence")
    categories = OrderedDict()#有序字典===# categories = {}#字典
    # 遍历37个频道
    for channel in channels:
        # 呈上  获取组id
        group_id = channel.group_id
        # 分组   防覆盖
        if group_id not in categories:
            # 'channels': []里装一级对象的数据# 'sub_cats': []里装的是二和三级数据
            categories[group_id] = {'channels': [], 'sub_cats': []}
        # 启下  获取一级对象
        cat1 = channel.category
        # 将一级分类数据按照前端格式构建
        # categories[group_id]['channels'].append({
        #     'id': cat1.id,
        #     'name': cat1.name,
        #     'url': channel.url
        # })
        # 动态添加url属性
        cat1.url = channel.url
        categories[group_id]['channels'].append(cat1)
        # 遍历所有二级分类
        for cat2 in cat1.subs.all():
            cat2.sub_cats = []
            # 遍历所有三级分类
            for cat3 in cat2.subs.all():
                # cat2.sub_cats.append({'id':cat3})
                cat2.sub_cats.append(cat3)
            categories[group_id]['sub_cats'].append(cat2)
    return categories
'''
OrderedDict([(1, {'channels': [<GoodsCategory: 手机>, <GoodsCategory: 相机>, <GoodsCateg 数码>],
 'sub_cats': [<GoodsCategory: 手机通讯>, <GoodsCategory: 手机配件>, <GoodsCatego <GoodsCategory: 数码配件>, <GoodsCategory: 影音娱乐>, <GoodsCategory: 智能设备>, <Goods教育>]}),
 (2, {'channels': [<GoodsCategory: 电脑>, <GoodsCategory: 办公>, <GoodsCategory>], 'sub_cats': [<GoodsCategory: 电脑整机>, <GoodsCategory: 电脑配件>, <GoodsCategory: 外oodsCategory: 办公设备>, <GoodsCategory: 文具耗材>, <GoodsCategory: 空调>, <GoodsCategorGoodsCategory: 冰箱>, <GoodsCategory: 生活电器>]}), (3, {'channels': [<GoodsCategory: 家odsCategory: 家具>, <GoodsCategory: 家装>, <GoodsCategory: 厨具>], 'sub_cats': [<GoodsCa: 生活日用>, <GoodsCategory: 家纺>, <GoodsCategory: 灯具>, <GoodsCategory: 家具>, <Goods家装软饰>, <GoodsCategory: 家装主材>, <GoodsCategory: 五金电工>, <GoodsCategory: 厨具>, : 厨房卫浴>]}), (4, {'channels': [<GoodsCategory: 男装>, <GoodsCategory: 女装>, <GoodsCa童装>, <GoodsCategory: 内衣>], 'sub_cats': [<GoodsCategory: 男装>, <GoodsCategory: 女装>oodsCategory: 童装>, <GoodsCategory: 童鞋>, <GoodsCategory: 内衣>, <GoodsCategory: 配饰> {'channels': [<GoodsCategory: 女鞋>, <GoodsCategory: 箱包>, <GoodsCategory: 钟表>, <Googory: 珠宝>], 'sub_cats': [<GoodsCategory: 时尚女鞋>, <GoodsCategory: 潮流女包>, <GoodsC品男包>, <GoodsCategory: 功能箱包>, <GoodsCategory: 钟表>, <GoodsCategory: 珠宝首饰>]}),ls': [<GoodsCategory: 男鞋>, <GoodsCategory: 运动>, <GoodsCategory: 户外>], 'sub_cats': sCategory: 流行男鞋>, <GoodsCategory: 运动鞋包>, <GoodsCategory: 运动服饰>, <GoodsCategoGoodsCategory: 骑行运动>, <GoodsCategory: 体育用品>, <GoodsCategory: 户外鞋服>, <GoodsCa: 户外装备>]}), (7, {'channels': [<GoodsCategory: 房产>, <GoodsCategory: 汽车>, <GoodsCa汽车用品>], 'sub_cats': [<GoodsCategory: 房产>, <GoodsCategory: 汽车车型>, <GoodsCategor<GoodsCategory: 汽车装饰>, <GoodsCategory: 车载电器>, <GoodsCategory: 安全自驾>]}), (8,  [<GoodsCategory: 母婴>, <GoodsCategory: 玩具乐器>], 'sub_cats': [<GoodsCategory: 奶粉>,ategory: 营养辅食>, <GoodsCategory: 尿裤湿斤>, <GoodsCategory: 玩具>, <GoodsCategory: 乐{'channels': [<GoodsCategory: 食品>, <GoodsCategory: 酒类>, <GoodsCategory: 生鲜>, <Goodory: 特产>], 'sub_cats': [<GoodsCategory: 新鲜水果>, <GoodsCategory: 蔬菜蛋品>, <GoodsCa肉类>, <GoodsCategory: 中外名酒>, <GoodsCategory: 海鲜水产>, <GoodsCategory: 冷饮冻食>, : 地方特产>]}), (10, {'channels': [<GoodsCategory: 图书>, <GoodsCategory: 音像>, <GoodsC 电子书>], 'sub_cats': [<GoodsCategory: 少儿>, <GoodsCategory: 教育>, <GoodsCategory: 文dsCategory: 科技>, <GoodsCategory: 音像>, <GoodsCategory: 电子书>]}), (11, {'channels': Category: 机票>, <GoodsCategory: 酒店>, <GoodsCategory: 旅游>, <GoodsCategory: 生活>], 'b_cats': [<GoodsCategory: 交通出行>, <GoodsCategory: 酒店预订>, <GoodsCategory: 旅游度假egory: 演出票务>, <GoodsCategory: 生活缴费>, <GoodsCategory: 生活服务>]})])

'''

